const mongoose = require('mongoose')
const Schema = mongoose.Schema

// create schema
const UserSchema = new Schema({
    username: {
        type: String,
        unique: true,
        trim: true,
        validate: {
            validator: value => {
                let reg = /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/;
                if (!value) {
                    return false
                } else if (value.length < 1 || value.length > 7) {
                    return false
                } else if (!reg.test(value)) {
                    return false
                } else {
                    return true
                }
            },
            message: "用户名不符合规则"
        }
    },
    useraccount: {
        type: String,
        unique: true,
        trim: true,
        validate: {
            validator: value => {
                let reg = /^[a-zA-z](?!^[\d]+$)(?!^[-]+$)(?!^[_]+$)(?!^[a-zA-Z]+$)[\w-]{6,30}$/
                if (!value) {
                    return false
                } else if (!reg.test(value)) {
                    return false
                } else {
                    return true
                }
            },
            message: "账号名不符合规则"
        }
    },
    password: {
        type: String,
        required: true,
        trim: true,
        validate: {
            validator: value => {
                let reg = /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,18}$/
                if (!value || !reg.test(value)) {
                    return false
                } else {
                    return true
                }
            },
            message: "密码不符合规则"
        }
    },
    logoUrl: {
        type: String,
        require: true,
        default: "http://localhost:8002/HP/IU.jpg",
    },

    phone: {
        type: String,
        trim: true,
        index: true,
        unique: true,
        required: true,
        match: /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/,
        message: '电话格式不对'
    },
    email: {
        type: String,
        default: '',
        validate: {
            validator: value => {
                let reg = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
                if (value == "") {
                    return true
                } else if (!reg.test(value)) {
                    return false
                } else {
                    return true
                }
            },
            message: "邮箱不符合规则"
        }

    },
    sex: {
        type: String,
        default: "3",
        validate: {
            validator: v => {
                if (v == '1' || v == '2' || v == '3') {
                    return true
                } else {
                    return false
                }
            },
            message: "性别错误"
        }
    },
    age: {
        type: Number,
        default: -1,
        min: -1,
        max: 200,
        message: "年龄在0-200之间的整数"
    },
    sendtime: "",
    smscode: '',
    address: {
        type: Array,
        default: [],
        minlength: 0,
        maxlength: 100,
    }
})

let userModel = mongoose.model('users', UserSchema)
module.exports = userModel